﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CellularAutomaton.Core;

namespace CellularAutomation.GUI
{
	public partial class Form1 : Form
	{
		private Grid _grid;
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			_grid = new Grid(40, 40);
			GetNewStatesData();
			//var dgvc = cellularDataGridView[0, 0];
			//cellularDataGridView.DataMember
		}

		private void GetNewStatesData() {
			this.cellularDataGridView.DataSource = this._grid.GetStateDataTable();
			foreach (DataGridViewRow row in this.cellularDataGridView.Rows)
			{
				row.Height = 15;
				foreach (DataGridViewCell cell in row.Cells)
				{
					var t = (ICellStateItem) cell.Value;
					cell.Style.BackColor = t.State ? Color.Black : Color.LightYellow;
					cell.ToolTipText = String.Format("X={0};Y={1}", ((ICellStateItem) cell.Value).X, ((ICellStateItem) cell.Value).Y);
				}
			}
			foreach (DataGridViewColumn column in this.cellularDataGridView.Columns)
				column.Width = 15;
		}

		private void cellularDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			//MessageBox.Show(String.Format("X={0};Y={1}", e.ColumnIndex, e.RowIndex));
			_grid.ChangeCellState(e.ColumnIndex, e.RowIndex);
			this.GetNewStatesData();
		}

		private void buttonNextStep_Click(object sender, EventArgs e)
		{
			//Stopwatch st = new Stopwatch();
			//st.Start();
			_grid.NextStep();
			this.GetNewStatesData();
			//st.Stop();
			//MessageBox.Show(st.ElapsedMilliseconds.ToString());
		}
	}
}
